<?php

/*
 *    Document   : users
 *   Created on : Jun 11, 2011, 3:55:22 PM
 *    Author     : ClarkT AKA FalleN
 *    Company    : Amodity
 *    Web        : http://www.amodity.com
 *    Copyright  : 2011
 */

/*
 *  Admin User Model
 */

class users_model extends CI_Model {

    public $tables = array();

    public function __construct() {
        parent::__construct();
        $this->load->config('auth', TRUE);
        $this->tables = $this->config->item('tables', 'auth');
    }

    function getusers() {

        //pull db info
        $this->load->library('auth');
        $userdata = $this->auth->get_users();

        $this->load->library('table');

        $this->table->set_heading('ID', 'Avatar', lang('admin_first_name'), lang('admin_last_name'), lang('admin_username'), lang('admin_email'), lang('admin_access'), lang('admin_active'), lang('admin_actions'));
        foreach ($userdata as $row) {
            $group = $this->auth->get_group($row->group_id);
            $links = anchor('admin/users/suspend_user/' . $row->id, '<span class="icon_single error_small_icon"></span>', array('class' => 'button white'));
            $links .= anchor('admin/users/edit_user/' . $row->id, '<span class="icon_single edit"></span>', array('class' => 'button white'));
            $links .= anchor('admin/users/delete_user/' . $row->id, '<span class="icon_single cancel"></span>', array('class' => 'button white'));
            $this->load->helper('user_image');

            $this->table->add_row(
                    $row->id, gravatar_link($row->email), $row->first_name, $row->last_name, $row->username, $row->email, $group->name, $row->active, $links
            );

            $this->table->set_template(array('table_open' => '<table class="display usermanage" id="tabledata">', 'row_alt_start' => '<tr class="odd">', 'row_alt_end' => '</tr>', 'table_close' => '</table>'));
        }
    }

    function do_edit() {
        //do user edits in database
        $data = array(
            'first_name' => $this->input->post('firstname'),
            'last_name' => $this->input->post('lastname'),
            'username' => $this->input->post('username'),
            'email' => $this->input->post('email'),
            'group_id' => $this->input->post('access'),
            'active' => $this->input->post('active')
        );
        $this->load->library('auth');
        if ($this->auth->update_user($this->input->post('id'), $data)) {
            $messages = $this->auth->messages();
            $this->message->set('success', $messages);
        } else {
            $errors = $this->auth->errors();
            $this->message->set('error', $errors);
        }
    }

    function get_user_status($status) {
        switch ($status) {
            case 'active':
                $this->db->where('active', '1');
                $this->db->from('users');
                $users = $this->db->count_all_results();
                $total = $this->db->count_all('users');
                $count1 = $users / $total;
                $count2 = $count1 * 100;
                $count = number_format($count2, 0);
                return array('total' => $users, 'percent' => $count);
                break;

            case 'pending':
                $this->db->where('activation_code !=', '');
                $this->db->from('users');
                $users = $this->db->count_all_results();
                $total = $this->db->count_all('users');
                $count1 = $users / $total;
                $count2 = $count1 * 100;
                $count = number_format($count2, 0);
                return array('total' => $users, 'percent' => $count);
                break;

            case 'suspended':
                $this->db->where('activation_code', null);
                $this->db->where('active', '0');
                $this->db->from('users');
                $users = $this->db->count_all_results();
                $total = $this->db->count_all('users');
                $count1 = $users / $total;
                $count2 = $count1 * 100;
                $count = number_format($count2, 0);
                return array('total' => $users, 'percent' => $count);
                break;

            default:
                return false;
                break;
        }
    }

    function user_delete($userid) {
        $this->db->where('id', $userid);
        $query = $this->db->delete('users');
        if ($query) {
            $this->message->set('success', lang('admin_userdelete_success'));
        } else {
            $this->message->set('error', lang('admin_userdelete_failed'));
        }
    }

    function usergroups() {

        //get info from db
        $this->db->join('permissions', $this->tables['groups'] . '.id = permissions.role_id');
        $query = $this->db->get($this->tables['groups']);

        //generate table
        $this->load->library('table');

        $this->table->set_heading('ID', lang('g_name'), lang('admin_description'), lang('cp_signin_allow'), lang('cp_upload_allow'), lang('cp_usercp_view'), lang('cp_usercp_edit'), lang('admin_images_view'), lang('admin_images_edit'), lang('admin_images_stats'), lang('admin_users_view'), lang('admin_users_manage'), lang('admin_roles_view'), lang('admin_roles_manage'), lang('admin_settings_view'), lang('admin_settings_manage'), lang('admin_database_view'), lang('admin_database_manage'), lang('admin_actions'));
        foreach ($query->result() as $row) {
            $links = anchor('admin/users/usergroups/edit_usergroup/' . $row->id, lang('g_edit')) . ' ';
            $links .= anchor('admin/delete_usergroup/' . $row->id, lang('g_delete'));

            $this->table->add_row(
                    $row->id, $row->name, $row->description, $links
            );

            $this->table->set_template(array('table_open' => '<table class="display usergroups">', 'row_alt_start' => '<tr class="odd">', 'row_alt_end' => '</tr>', 'table_close' => '</table>'));
        }
    }

}

?>